<template>
  <div class="wrap">
    <div v-if="isMainPanel">
      <div>
        <el-button type="primary"
                   style="margin-bottom: 20px"
                   @click="openParamPanel(false,false)">
          {{ $t('message.SeparationFace.createFace') }}
        </el-button>
        <div>
          <span> {{ $t('message.SeparationFace.searchName') }}：</span>
          <el-input style="width: 200px;margin-bottom: 15px"
                    v-model="searchName">
          </el-input>
        </div>
        <el-table
            ref="faceTable"
            :data="currentData"
            @row-click="highLightByRow"
            highlight-current-row
            height="400"
            style="width: 100%">
          <el-table-column
              prop="surfaceName"
              :label=rowName
              width="150">
          </el-table-column>
          <el-table-column
              prop="surfaceType"
              :label=rowType
              width="80">
          </el-table-column>
          <el-table-column
              width="120">
            <template v-slot="scope">
              <div v-if="scope.row.docType === 'Edit'">
                <el-button type="text"
                           size="small"
                           class="tableButton"
                           @click="editSurface(scope.row.id)">
                  {{ $t('message.SeparationFace.rowEdit') }}
                </el-button>
                <el-button type="text"
                           size="small"
                           class="tableButton"
                           @click="delSurface(scope.row.id,scope.row.featureId)">
                  {{ $t('message.SeparationFace.rowDelete') }}
                </el-button>
              </div>
              <div v-if="scope.row.docType === 'unEdit'">
                <el-button type="text"
                           size="small"
                           class="tableButton"
                           @click="visiableFeature(scope.row.activeDocId,scope.row.featureId)">
                  {{ scope.row.visible ? $t('message.SeparationFace.hide') : $t('message.SeparationFace.show') }}
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="btn">
        <el-button
            type="primary"
            size="small"
            @click="close">
          {{ $t('message.SeparationFace.close') }}
        </el-button>
      </div>
    </div>

    <div v-if="!isMainPanel">
      <div style="margin-bottom: 10px">
        <div style="height: 26px;margin: 7px 0;">
          <span>{{ $t('message.SeparationFace.operType') }}:</span>
          <el-select v-model="separationFaceCreatedType"
                     @change="selectChanged"
                     :disabled="rowEdit">
            <el-option
                v-for="item in separationFaceOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </div>
        <div style="height: 26px;margin: 7px 0;">
          <span>名称:</span>
          <el-input class="input"
                    v-model="separationFace.name"
                    style="width: 150px"></el-input>
        </div>
      </div>

      <div v-if="separationFaceCreatedType !=='三点'
      && separationFaceCreatedType!=='点法向'
      && separationFaceCreatedType!=='线角度'"
           class="d-flex"
           style="width: 90%">
        <query-list
            v-model="pickParams.elements"
            :tipMsg="pickParams.message"
        />
      </div>

      <div v-if="separationFaceCreatedType === '三点'">
        <div class="pointCoor">
          <span class="title">{{ $t('message.SeparationFace.point') }} A</span>
          <div>
            x:
            <el-input type="number" class="input" v-model="threePoints.A.x"></el-input>
            mm
          </div>
          <div>
            y:
            <el-input type="number" class="input" v-model="threePoints.A.y"></el-input>
            mm
          </div>
          <div>
            z:
            <el-input type="number" class="input" v-model="threePoints.A.z"></el-input>
            mm
          </div>
        </div>
        <div class="pointCoor">
          <span class="title">{{ $t('message.SeparationFace.point') }} B</span>
          <div>
            x:
            <el-input type="number" class="input" v-model="threePoints.B.x"></el-input>
            mm
          </div>
          <div>
            y:
            <el-input type="number" class="input" v-model="threePoints.B.y"></el-input>
            mm
          </div>
          <div>
            z:
            <el-input type="number" class="input" v-model="threePoints.B.z"></el-input>
            mm
          </div>
        </div>
        <div class="pointCoor">
          <span class="title">{{ $t('message.SeparationFace.point') }} C</span>
          <div>
            x:
            <el-input type="number" class="input" v-model="threePoints.C.x"></el-input>
            mm
          </div>
          <div>
            y:
            <el-input type="number" class="input" v-model="threePoints.C.y"></el-input>
            mm
          </div>
          <div>
            z:
            <el-input type="number" class="input" v-model="threePoints.C.z"></el-input>
            mm
          </div>
        </div>
        <div class="separation_cb_height">
          <el-checkbox v-model="threePoints.invertNormal" :label=overturn></el-checkbox>
        </div>

      </div>

      <div v-if="separationFaceCreatedType === '偏移'">
        <div class="separation_cb_height">
          <span>{{ $t('message.SeparationFace.distinct') }}：</span>
          <el-input type="number"
                    class="input"
                    v-model="distinctParam.paralDis"
                    @change="validateDistinct"
                    style="width: 140px"></el-input>
          mm
        </div>
        <div class="separation_cb_height">
          <el-checkbox v-model="distinctParam.invertNormal" :label=overturn></el-checkbox>
        </div>
        <div class="separation_cb_height">
          <el-checkbox v-model="distinctParam.reverse" :label=reverse></el-checkbox>
        </div>
      </div>

      <div v-if="separationFaceCreatedType === '平面点'">
        <div class="pointCoor">
          <span class="title">{{ $t('message.SeparationFace.point') }}：</span>
          <div>
            x:
            <el-input type="number" class="input" v-model="planePointParam.paralPnt.x"></el-input>
            mm
          </div>
          <div>
            y:
            <el-input type="number" class="input" v-model="planePointParam.paralPnt.y"></el-input>
            mm
          </div>
          <div>
            z:
            <el-input type="number" class="input" v-model="planePointParam.paralPnt.z"></el-input>
            mm
          </div>
        </div>
        <div class="separation_cb_height">
          <el-checkbox v-model="planePointParam.invertNormal" :label=overturn></el-checkbox>
        </div>
      </div>

      <div v-if="separationFaceCreatedType === '平面角度'">
        <div class="pointCoor">
          <span class="title">{{ $t('message.SeparationFace.axisStartPoint') }}：</span>
          <div>
            x:
            <el-input type="number" class="input" v-model="planeAngleParam.point1.x"></el-input>
            mm
          </div>
          <div>
            y:
            <el-input type="number" class="input" v-model="planeAngleParam.point1.y"></el-input>
            mm
          </div>
          <div>
            z:
            <el-input type="number" class="input" v-model="planeAngleParam.point1.z"></el-input>
            mm
          </div>
        </div>
        <div class="pointCoor">
          <span class="title">{{ $t('message.SeparationFace.axisEndPoint') }}：</span>
          <div>
            x:
            <el-input type="number" class="input" v-model="planeAngleParam.point2.x"></el-input>
            mm
          </div>
          <div>
            y:
            <el-input type="number" class="input" v-model="planeAngleParam.point2.y"></el-input>
            mm
          </div>
          <div>
            z:
            <el-input type="number" class="input" v-model="planeAngleParam.point2.z"></el-input>
            mm
          </div>
        </div>
        <div>
          <span>{{ $t('message.SeparationFace.axisAngle') }}：</span>
          <el-input type="number" class="input" style="width: 55%;margin-bottom: 5px"
                    v-model="planeAngleParam.axisAngle"></el-input>
          °
        </div>
        <div class="separation_cb_height">
          <el-checkbox v-model="planeAngleParam.invertNormal" :label=overturn></el-checkbox>
        </div>
        <div class="separation_cb_height">
          <el-checkbox v-model="planeAngleParam.reverse" :label=reverse></el-checkbox>
        </div>
      </div>

      <div v-if="separationFaceCreatedType === '点法向'">
        <div class="pointCoor">
          <span class="title">{{ $t('message.SeparationFace.axisStartPoint') }}：</span>
          <div>
            x:
            <el-input type="number" class="input" v-model="pointNormalParam.point1.x"></el-input>
            mm
          </div>
          <div>
            y:
            <el-input type="number" class="input" v-model="pointNormalParam.point1.y"></el-input>
            mm
          </div>
          <div>
            z:
            <el-input type="number" class="input" v-model="pointNormalParam.point1.z"></el-input>
            mm
          </div>
        </div>
        <div class="pointCoor">
          <span class="title">{{ $t('message.SeparationFace.axisEndPoint') }}：</span>
          <div>
            x:
            <el-input type="number" class="input" v-model="pointNormalParam.point2.x"></el-input>
            mm
          </div>
          <div>
            y:
            <el-input type="number" class="input" v-model="pointNormalParam.point2.y"></el-input>
            mm
          </div>
          <div>
            z:
            <el-input type="number" class="input" v-model="pointNormalParam.point2.z"></el-input>
            mm
          </div>
        </div>
        <div class="pointCoor">
          <span class="title">{{ $t('message.SeparationFace.point') }}：</span>
          <div>
            x:
            <el-input type="number" class="input" v-model="pointNormalParam.planePnt.x"></el-input>
            mm
          </div>
          <div>
            y:
            <el-input type="number" class="input" v-model="pointNormalParam.planePnt.y"></el-input>
            mm
          </div>
          <div>
            z:
            <el-input type="number" class="input" v-model="pointNormalParam.planePnt.z"></el-input>
            mm
          </div>
        </div>
        <div class="separation_cb_height">
          <el-checkbox v-model="pointNormalParam.invertNormal" :label=overturn></el-checkbox>
        </div>
      </div>

      <div v-if="separationFaceCreatedType === '线角度'">
        <div class="pointCoor">
          <span class="title">{{ $t('message.SeparationFace.axisStartPoint') }}：</span>
          <div>
            x:
            <el-input type="number" class="input" v-model="lineAngleParam.point1.x"></el-input>
            mm
          </div>
          <div>
            y:
            <el-input type="number" class="input" v-model="lineAngleParam.point1.y"></el-input>
            mm
          </div>
          <div>
            z:
            <el-input type="number" class="input" v-model="lineAngleParam.point1.z"></el-input>
            mm
          </div>
        </div>
        <div class="pointCoor">
          <span class="title">{{ $t('message.SeparationFace.axisEndPoint') }}：</span>
          <div>
            x:
            <el-input type="number" class="input" v-model="lineAngleParam.point2.x"></el-input>
            mm
          </div>
          <div>
            y:
            <el-input type="number" class="input" v-model="lineAngleParam.point2.y"></el-input>
            mm
          </div>
          <div>
            z:
            <el-input type="number" class="input" v-model="lineAngleParam.point2.z"></el-input>
            mm
          </div>
        </div>
        <div class="pointCoor">
          <span class="title">{{ $t('message.SeparationFace.point') }}：</span>
          <div>
            x:
            <el-input type="number" class="input" v-model="lineAngleParam.planePnt.x"></el-input>
            mm
          </div>
          <div>
            y:
            <el-input type="number" class="input" v-model="lineAngleParam.planePnt.y"></el-input>
            mm
          </div>
          <div>
            z:
            <el-input type="number" class="input" v-model="lineAngleParam.planePnt.z"></el-input>
            mm
          </div>
        </div>
        <div>
          {{ $t('message.SeparationFace.axisAngle') }}：
          <el-input type="number" style="width: 55%;margin-bottom: 5px" v-model="lineAngleParam.axisAngle"></el-input>
          °
        </div>
        <div class="separation_cb_height">
          <el-checkbox v-model="lineAngleParam.invertNormal" :label=overturn></el-checkbox>
        </div>
        <div class="separation_cb_height">
          <el-checkbox v-model="lineAngleParam.reverse" :label=reverse></el-checkbox>
        </div>
      </div>

      <div style="margin-top: 10px" class="btn">
        <el-button
            v-if="!rowEdit"
            type="primary"
            size="small"
            @click="addSurface(0)"
        >{{ $t('message.SeparationFace.created') }}
        </el-button>

        <el-button
            v-if="rowEdit"
            type="primary"
            size="small"
            @click="updateSurface"
        >{{ $t('message.SeparationFace.modified') }}
        </el-button>

        <el-button
            type="primary"
            size="small"
            @click="openParamPanel(true,false)"
        >{{ $t('message.SeparationFace.returnToMain') }}
        </el-button>
      </div>

    </div>
  </div>

</template>
<script>
import separationFaceCommand from "./SeparationFaceCommand";
import DialogMixin from "../DialogMixin";
import {Axis, Point} from "../../../../nodeApp/static/lib/crowncad-plugin-sdk-0.0.1";
import {ElementType, EntityType} from "../../../modeling/TypeDefine";

export default {
  name: "separationFace",
  mixins: [DialogMixin],
  data() {
    return {
      /**
       * 元素拾取
       */
      pickParams: {
        elements: [],
        message: this.$t('message.SeparationFace.selectFace')
      },
      /**
       * 分离面数据
       */
      separatorSurfaceList: [],
      /**
       * 三点创建平面参数
       */
      threePoints: {
        A: {
          x: 0, y: 0, z: 0
        },
        B: {
          x: 0, y: 0, z: 0
        },
        C: {
          x: 0, y: 0, z: 0
        },
        invertNormal: false,  // 法向翻转，0不翻转，1翻转
      },
      /**
       * 偏离距离创建分离面参数
       */
      distinctParam: {
        elementId: 0, // 基准面id或平面id
        invertNormal: false,  // 法向翻转，0不翻转，1翻转
        paralDis: 0.0,    // 平行偏移距离
        reverse: false        // 是否反向，0不反向，1反向
      },
      /**
       * 平面点创建分离面参数
       */
      planePointParam: {
        elementId: 0, // 基准面id或平面id
        invertNormal: false, // 法向翻转，0不翻转，1翻转
        paralPnt: {
          x: 0,
          y: 0,
          z: 0
        } // 点Point
      },
      /**
       * 平面角度建面参数
       */
      planeAngleParam: {
        elementId: 0, // 基准面id或平面id
        invertNormal: false, // 法向翻转，0不翻转，1翻转,
        point1: {
          x: 0, y: 0, z: 0
        },
        point2: {
          x: 0, y: 0, z: 0
        },
        axisAngle: 0.0,
        reverse: false        // 是否反向，0不反向，1反向
      },
      /**
       * 点法向建面参数
       */
      pointNormalParam: {
        elementId: 0,
        invertNormal: false, // 法向翻转，0不翻转，1翻转,
        point1: {
          x: 0, y: 0, z: 0
        },
        point2: {
          x: 0, y: 0, z: 0
        },
        planePnt: {
          x: 0,
          y: 0,
          z: 0
        }
      },
      /**
       * 线角度创建分离面参数
       */
      lineAngleParam: {
        elementId: 0,
        invertNormal: false, // 法向翻转，0不翻转，1翻转,
        reverse: false,       // 是否反向，0不反向，1反向
        axisAngle: 0.0,
        point1: {
          x: 0, y: 0, z: 0
        },
        point2: {
          x: 0, y: 0, z: 0
        },
        planePnt: {
          x: 0,
          y: 0,
          z: 0
        }
      },
      /**
       * 基础参数
       */
      separationFace: {
        name: "",
        id: "",
        featureId: 0,
        entityId: 0,
        fromId: 0,
        index: 0
      },
      /**
       * 创建类型
       */
      separationFaceCreatedType: "三点",
      /**
       * 创建类型集合
       */
      separationFaceOptions: [
        {
          value: '三点',
          label: this.$t('message.SeparationFace.threePoints')
        }, {
          value: '偏移',
          label: this.$t('message.SeparationFace.offset')
        }, {
          value: '平面点',
          label: this.$t('message.SeparationFace.planePoint')
        }, {
          value: '平面角度',
          label: this.$t('message.SeparationFace.planeAngle')
        }, {
          value: '点法向',
          label: this.$t('message.SeparationFace.pointNormal')
        }, {
          value: '线角度',
          label: this.$t('message.SeparationFace.lineAngle')
        }
      ],
      /**
       * 搜索分离面名称
       */
      searchName: "",
      /**
       * 明细界面是否处于编辑模式
       */
      rowEdit: false,
      /**
       * 当前是否为查询界面
       */
      isMainPanel: true,
      overturn: this.$t('message.SeparationFace.overturn'),
      reverse: this.$t('message.SeparationFace.reverse'),
      rowName: this.$t('message.SeparationFace.name'),
      rowType: this.$t('message.SeparationFace.operType'),
    }
  },
  mounted() {
    // 初始化界面
    separationFaceCommand.init(500, 500, 550, async () => {
      // 获取工作模式
      await separationFaceCommand.getWorkSpaceMode([]);
      // 获取项目和文档信息
      await separationFaceCommand.getProjectInfo().then(() => {
        // 查询分离面明细数据
        this.getSeparationData();
      });
      // 定义拾取命令
      this.pickMode();
      // 调整命令框的大小
      this.changePluginContainerHeight();
    });
  },
  methods: {
    /**
     * 控制页面展示的高宽
     */
    changePluginContainerHeight() {
      separationFaceCommand.changePluginContainerHeight(
          {minWidth: 200, maxHeight: this.$el.clientHeight + 10, width: this.isMainPanel ? 400 : 250});
    },

    /**
     * 获取分离面数据
     */
    getSeparationData() {
      return separationFaceCommand.getSeparationSurfaces().then(() => {
        this.separatorSurfaceList = [];
        separationFaceCommand.separationList.forEach(data => {
          this.separatorSurfaceList.push(data)
        })
      });
    },

    /**
     * 拾取逻辑
     */
    pickMode() {
      if (separationFaceCommand.workSpaceMode === "PartDocument"
          || separationFaceCommand.workSpaceMode === "AssemblyDocument") {
        separationFaceCommand.handlePickFaceInfo([ElementType.FACE, ElementType.DATUMPLANE],elements => {
          this.pickParams.elements = elements;
          if (elements && elements?.length === 0) {
            this.$refs?.faceTable?.setCurrentRow(null)
          }
          if (elements && (!elements[0]?.versionId || elements[0]?.versionId === separationFaceCommand.activeDocId)) {
            //debugger
            elements?.forEach(element => {
              if (element?.entityInfo) {
                this.highLightRow(element?.entityInfo.featureId)
              }
            })
          }
        });
      }
    },

    /**
     * 高亮选中的表格行
     * @param featureId
     */
    highLightRow(featureId) {
      //取消当前高亮效果
      separationFaceCommand.hightLightElement(false)
      if (this.isMainPanel) {
        let face = this.separatorSurfaceList.find(surface => surface.featureId === featureId);
        if (!!face) {
          this.$refs.faceTable?.setCurrentRow(face, true)
          // 表格滚动逻辑
          let rowIndex = this.separatorSurfaceList.findIndex(surface => surface.featureId === face.featureId)
          this.scrollTable(this.$refs.faceTable, rowIndex)
        } else {
          this.$refs?.faceTable?.setCurrentRow(null)
        }
      }
    },

    /**
     * 滚动到表格指定位置
     * @param tableElement
     * @param rowIndex
     */
    scrollTable(tableElement, rowIndex) {
      let rows = tableElement.bodyWrapper.querySelectorAll('.el-table__body tbody .el-table__row');
      let scrollTop = 0;
      for (let i = 0; i < rows.length; i++) {
        if (i === rowIndex) {
          break;
        }
        scrollTop += rows[i].offsetHeight;
      }
      tableElement.bodyWrapper.scrollTop = scrollTop;
    },

    /**
     * 高亮元素
     */
    highLightByRow(row) {
      separationFaceCommand.hightLightElement(true, row.entityId,
          row.activeDocId, row.featureId, row.instanceId)
    },

    /**
     * 关闭窗口
     */
    close() {
      separationFaceCommand.closeActiveCmd();
    },

    /**
     * 三点创建分离面
     */
    async createPlaneByThreePoints() {
      // 绘制基本面
      let point1 = new Point(this.threePoints.A.x, this.threePoints.A.y, this.threePoints.A.z);
      let point2 = new Point(this.threePoints.B.x, this.threePoints.B.y, this.threePoints.B.z);
      let point3 = new Point(this.threePoints.C.x, this.threePoints.C.y, this.threePoints.C.z);
      separationFaceCommand.command.datum.createPlaneByThreePoints({
        threePnts: [point1, point2, point3],
        invertNormal: this.threePoints.invertNormal ? 1 : 0
      }, this.separationFace.name)
      await separationFaceCommand.command.execute();
      separationFaceCommand.command.clearCommand();
      return {
        commandParams: this.threePoints,
        msg: ""
      }
    },

    /**
     * 偏移创建分离面
     */
    async createPlaneByDistinct() {
      if (this.pickParams.elements[0]) {
        let type = this.pickParams.elements[0].type;
        if (type !== EntityType.FACE && type !== EntityType.DATUMPLANE) {
          return {
            commandParams: "",
            msg: this.$t('message.SeparationFace.cannotCreated')
          }
        }
        let id = this.pickParams.elements[0].id;
        this.distinctParam.elementId = id;
        let param = {
          invertNormal: this.distinctParam.invertNormal ? 1 : 0,
          paralDis: this.distinctParam.paralDis,
          reverse: this.distinctParam.reverse ? 1 : 0,
        };
        if (type === EntityType.FACE) {
          param.faceId = id;
        }
        if (type === EntityType.DATUMPLANE) {
          param.planeId = id;
        }
        separationFaceCommand.command.datum.createPlaneByDistance(param, this.separationFace.name)
        await separationFaceCommand.command.execute();
        separationFaceCommand.command.clearCommand();
        return {
          commandParams: this.distinctParam,
          msg: ""
        }
      } else {
        return {
          commandParams: "",
          msg: this.$t('message.SeparationFace.shouldSelectFace')
        }
      }
    },

    /**
     * 平面点创建分离面
     */
    async createPlaneByPlanePoint() {
      if (this.pickParams.elements[0]) {
        let type = this.pickParams.elements[0].type;
        if (type !== EntityType.FACE && type !== EntityType.DATUMPLANE) {
          return {
            commandParams: "",
            msg: this.$t('message.SeparationFace.cannotCreated')
          }
        }
        let id = this.pickParams.elements[0].id;
        this.planePointParam.elementId = id;
        let param = {
          invertNormal: this.planePointParam.invertNormal ? 1 : 0,
          paralPnt: new Point(this.planePointParam.paralPnt.x, this.planePointParam.paralPnt.y, this.planePointParam.paralPnt.z)
        };
        if (type === EntityType.FACE) {
          param.faceId = id;
        }
        if (type === EntityType.DATUMPLANE) {
          param.planeId = id;
        }
        separationFaceCommand.command.datum.createPlaneByPlanePoint(param, this.separationFace.name)
        await separationFaceCommand.command.execute();
        separationFaceCommand.command.clearCommand();
        return {
          commandParams: this.planePointParam,
          msg: ""
        };
      } else {
        return {
          commandParams: "",
          msg: this.$t('message.SeparationFace.shouldSelectFace')
        }
      }
    },

    /**
     * 平面角度创建分离面
     */
    async createPlaneByPlaneAngle() {
      if (this.pickParams.elements[0]) {
        let type = this.pickParams.elements[0].type;
        if (type !== EntityType.FACE && type !== EntityType.DATUMPLANE) {
          return {
            commandParams: "",
            msg: this.$t('message.SeparationFace.cannotCreated')
          }
        }
        let id = this.pickParams.elements[0].id;
        this.planeAngleParam.elementId = id;
        let axis = new Axis();
        axis.setPoint(new Point(this.planeAngleParam.point1.x, this.planeAngleParam.point1.y, this.planeAngleParam.point1.z),
            new Point(this.planeAngleParam.point2.x, this.planeAngleParam.point2.y, this.planeAngleParam.point2.z));
        let param = {
          invertNormal: this.planeAngleParam.invertNormal ? 1 : 0,
          reverse: this.planeAngleParam.reverse ? 1 : 0,
          axisAngle: this.planeAngleParam.axisAngle,
          additionalAxis: axis
        };
        if (type === EntityType.FACE) {
          param.faceId = id;
        }
        if (type === EntityType.DATUMPLANE) {
          param.planeId = id;
        }
        separationFaceCommand.command.datum.createPlaneByPlaneAngle(param, this.separationFace.name)
        await separationFaceCommand.command.execute();
        separationFaceCommand.command.clearCommand();
        return {
          commandParams: this.planeAngleParam,
          msg: ""
        };
      } else {
        return {
          commandParams: "",
          msg: this.$t('message.SeparationFace.shouldSelectFace')
        }
      }
    },

    /**
     * 点法向创建分离面
     */
    async createPlaneByPointNormal() {
      let axis = new Axis();
      axis.setPoint(new Point(this.pointNormalParam.point1.x, this.pointNormalParam.point1.y, this.pointNormalParam.point1.z),
          new Point(this.pointNormalParam.point2.x, this.pointNormalParam.point2.y, this.pointNormalParam.point2.z));
      let param = {
        invertNormal: this.pointNormalParam.invertNormal ? 1 : 0,
        planeAxis: axis,
        planePnt: new Point(this.pointNormalParam.planePnt.x, this.pointNormalParam.planePnt.y, this.pointNormalParam.planePnt.z)
      };
      separationFaceCommand.command.datum.createPlaneByPointNormal(param, this.separationFace.name)
      await separationFaceCommand.command.execute();
      separationFaceCommand.command.clearCommand();
      return {
        commandParams: this.pointNormalParam,
        msg: ""
      };
    },

    /**
     * 线角度创建分离面
     * @returns {Promise<{elementId: number, reverse: boolean, axisAngle: number, invertNormal: boolean, point1: {x: number, y: number, z: number}, planePnt: {x: number, y: number, z: number}, point2: {x: number, y: number, z: number}}>}
     */
    async createPlaneByLineAngle() {
      let axis = new Axis();
      axis.setPoint(new Point(this.lineAngleParam.point1.x, this.lineAngleParam.point1.y, this.lineAngleParam.point1.z),
          new Point(this.lineAngleParam.point2.x, this.lineAngleParam.point2.y, this.lineAngleParam.point2.z));
      let param = {
        invertNormal: this.lineAngleParam.invertNormal ? 1 : 0,
        planeAxis: axis,
        planePnt: new Point(this.lineAngleParam.planePnt.x, this.lineAngleParam.planePnt.y, this.lineAngleParam.planePnt.z),
        reverse: this.lineAngleParam.reverse ? 1 : 0,
        axisAngle: this.lineAngleParam.axisAngle,
      };
      separationFaceCommand.command.datum.createPlaneByLineAngle(param, this.separationFace.name)
      await separationFaceCommand.command.execute();
      separationFaceCommand.command.clearCommand();
      return {
        commandParams: this.lineAngleParam,
        msg: ""
      };
    },

    /**
     * 新增分离面
     * @returns {Promise<void>}
     */
    async addSurface(currentIndex) {
      // 分离面名称验证
      let valid = await this.validateFaceName("add");
      if (!valid) {
        return
      }
      // 调用具体方法
      let createParam = "";
      switch (this.separationFaceCreatedType) {
        case "三点":
          createParam = await this.createPlaneByThreePoints();
          break;
        case "平面点":
          createParam = await this.createPlaneByPlanePoint();
          break;
        case "线角度":
          createParam = await this.createPlaneByLineAngle();
          break;
        case "点法向":
          createParam = await this.createPlaneByPointNormal();
          break;
        case "平面角度":
          createParam = await this.createPlaneByPlaneAngle();
          break;
        case "偏移":
          createParam = await this.createPlaneByDistinct();
          break;
        default:
          return;
      }
      if (!!createParam.msg) {
        separationFaceCommand.eventBusAlertMsg(createParam.msg, "warning")
        return;
      }

      if (!createParam.commandParams) {
        separationFaceCommand.eventBusAlertMsg(this.$t('message.SeparationFace.errorCreated'), "error")
        return;
      }
      // 获取特征
      let entities = [];
      let faceFeature = await separationFaceCommand.query.getEntitiesByFeatureNames([this.separationFace.name]);
      if (faceFeature.code === 200) {
        entities = faceFeature.data;
      }
      // 如果没有特征id，则创建失败
      if (!entities?.entities[0]?.featureId) {
        separationFaceCommand.eventBusAlertMsg(this.$t('message.SeparationFace.errorCreated'), "error")
        return;
      }
      // 添加表格数据
      separationFaceCommand.addSeparationSurface(this.separationFace.name, JSON.stringify(createParam.commandParams),
          entities.entities[0].featureId, entities.entities[0].id, this.separationFaceCreatedType,
          createParam.commandParams?.elementId ?? 0, currentIndex ?? 0).then(() => {
        separationFaceCommand.eventBusAlertMsg(this.$t('message.SeparationFace.successCreated'), "success")
        this.getSeparationData().then(() => {
          let newFace = this.separatorSurfaceList.find(face => face.entityId === entities.entities[0].id);
          if (!!newFace) {
            this.separationFace.id = newFace.id;
            this.separationFace.featureId = newFace.featureId;
            this.separationFace.entityId = newFace.entityId;
            this.separationFace.fromId = newFace.fromId;
            this.separationFace.index = newFace.index;
          }
        });
        this.separationFace.name = "";
      })
    },

    /**
     * 更新分离面
     */
    async updateSurface() {
      let valid = await this.validateFaceName("modify");
      if (!valid) {
        return
      }
      let currentIndex = this.separationFace.index;
      await this.delSurface(this.separationFace.id, this.separationFace.featureId, () => {

      })
      await this.addSurface(currentIndex);
    },

    /**
     * 删除分离面
     * @param id
     * @param fid
     * @param addCb
     */
    async delSurface(id, fid, operate, addCb) {
      await separationFaceCommand.deleteFeatures(fid, id, operate, addCb);
      await this.getSeparationData();
    },

    /**
     * 编辑数据
     * @param id
     */
    editSurface(id) {
      this.openParamPanel(false, true);

      let face = this.separatorSurfaceList.find(face => face.id === id);
      if (!!face) {
        this.separationFaceCreatedType = face.surfaceType
        this.separationFace.name = face.surfaceName;
        this.separationFace.id = face.id;
        this.separationFace.featureId = face.featureId;
        this.separationFace.entityId = face.entityId;
        this.separationFace.index = face.surfaceIndex;
        this.separationFace.fromId = face.fromId;

        // 处理操作类型
        if (!!face.createParam) {
          switch (this.separationFaceCreatedType) {
            case "三点":
              this.threePoints = JSON.parse(face.createParam);
              break;
            case "平面点":
              this.planePointParam = JSON.parse(face.createParam);
              this.planePointParam.paralPnt.x = 10
              this.getFaceByFromId(this.planePointParam.elementId);
              break;
            case "线角度":
              this.lineAngleParam = JSON.parse(face.createParam);
              break;
            case "点法向":
              this.pointNormalParam = JSON.parse(face.createParam);
              break;
            case "平面角度":
              this.planeAngleParam = JSON.parse(face.createParam);
              this.getFaceByFromId(this.planeAngleParam.elementId);
              break;
            case "偏移":
              this.distinctParam = JSON.parse(face.createParam);
              this.getFaceByFromId(this.distinctParam.elementId);
              break;
            default:
              return;
          }
        }
      }
    },

    /**
     * 面板操作
     * @param isOpenMainPanel
     * @param isRowEdit
     */
    openParamPanel(isOpenMainPanel, isRowEdit) {
      this.reSetData(false);
      this.isMainPanel = true;
      this.rowEdit = false;
      if (!isOpenMainPanel) {
        this.isMainPanel = false;
      }
      if (isRowEdit) {
        this.rowEdit = true;
      }
    },

    /**
     * 更新拾取数据
     * @param id
     */
    getFaceByFromId(id) {
      separationFaceCommand.getElementById(id, (res) => {
        if (res && res[0]) {
          this.pickParams.elements = res;
        }
      });
    },

    /**
     * 数据还原
     */
    reSetData(isTab) {
      if (!isTab) {
        this.separationFaceCreatedType = "三点";
      }
      this.separationFace.name = "";
      this.separationFace.id = "";
      this.separationFace.featureId = 0;
      this.separationFace.entityId = 0;
      this.separationFace.index = 0;
      this.separationFace.fromId = 0;
      this.pickParams.elements = [];
      // 参数还原
      this.distinctParam = {
        elementId: 0,
        invertNormal: false,
        paralDis: 0.0,
        reverse: false
      };
      this.planeAngleParam = {
        elementId: 0,
        invertNormal: false,
        point1: {
          x: 0, y: 0, z: 0
        },
        point2: {
          x: 0, y: 0, z: 0
        },
        axisAngle: 0.0,
        reverse: false
      };
      this.planePointParam = {
        elementId: 0,
        invertNormal: false,
        paralPnt: {
          x: 0,
          y: 0,
          z: 0
        }
      };
      this.threePoints = {
        A: {
          x: 0, y: 0, z: 0
        },
        B: {
          x: 0, y: 0, z: 0
        },
        C: {
          x: 0, y: 0, z: 0
        },
        invertNormal: false,
      };
      this.lineAngleParam = {
        elementId: 0,
        invertNormal: false,
        reverse: false,
        axisAngle: 0.0,
        point1: {
          x: 0, y: 0, z: 0
        },
        point2: {
          x: 0, y: 0, z: 0
        },
        planePnt: {
          x: 0,
          y: 0,
          z: 0
        }
      }
      this.pointNormalParam = {
        elementId: 0,
        invertNormal: false,
        point1: {
          x: 0, y: 0, z: 0
        },
        point2: {
          x: 0, y: 0, z: 0
        },
        planePnt: {
          x: 0,
          y: 0,
          z: 0
        }
      }

    },

    /**
     * 距离判断
     * @param value
     */
    validateDistinct(value) {
      if (value > 10000000) {
        separationFaceCommand.eventBusAlertMsg(this.$t('message.SeparationFace.distinctExceedError'), "warning")
        return
      }
      if (value < 0) {
        separationFaceCommand.eventBusAlertMsg(this.$t('message.SeparationFace.distinctLessThanZeroError'), "warning")
        return
      }
    },

    /**
     * 名称验证
     * @param opearte
     */
    async validateFaceName(opearte) {
      // 必须有名称
      if (!this.separationFace.name) {
        separationFaceCommand.eventBusAlertMsg(this.$t('message.SeparationFace.nameUndefined'), "warning")
        return false
      }

      // 控制名称的长度
      if (this.separationFace.name.length > 50) {
        separationFaceCommand.eventBusAlertMsg(this.$t('message.SeparationFace.nameExceed'), "warning")
        return false
      }

      // 名称不可以存在
      if (opearte === "add") {
        let currentFeature = await separationFaceCommand.getAllFeatures()
        if (currentFeature.find(f => f.name === this.separationFace.name)) {
          separationFaceCommand.eventBusAlertMsg(this.$t('message.SeparationFace.nameExist'), "warning")
          return false
        }
      }

      return true;
    },
    /**
     * 装配下特征显隐处理
     * @param docId
     * @param featureId
     */
    visiableFeature(docId, featureId) {
      let faces = this.separatorSurfaceList.filter(face => face.activeDocId === docId && face.featureId === featureId);
      if (!!faces && faces.length > 0) {
        faces.forEach(face => face.visible = !face.visible);
        separationFaceCommand.visiableFeature(docId, featureId, faces[0].visible)
      }
    },
    /**
     * 切换tab
     */
    selectChanged() {
      this.reSetData(true);
    }
  },
  computed: {
    currentData() {
      if (!this.searchName) {
        return this.separatorSurfaceList;
      } else {
        return this.separatorSurfaceList.filter(face => face.surfaceName.indexOf(this.searchName) >= 0)
      }

    }
  }
}

</script>
<style scoped>
.wrap {
  font-size: 12px;
  padding-left: 5px;
}

.tableButton {
  color: #1b5faa;
}

.pointCoor {
  padding-bottom: 10px;
}

.pointCoor .input {
  width: 150px;
}

.pointCoor .title {
  font-weight: bolder;
}

.separation_cb_height {
  height: 32px;
  line-height: 32px;
}

>>> .el-checkbox__label {
  padding-left: 5px;
}

</style>